package com.liu.stockmarket.ui.kline

import androidx.lifecycle.*
import com.liu.stockmarket.model.StockInfo
import com.liu.stockmarket.repository.network.EasyMoneyRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.lang.ClassCastException

class KlineViewModel(private val easy: EasyMoneyRepository) : ViewModel() {

    private val _stockInfo = MutableLiveData<StockInfo>()

    val stockInfo: LiveData<StockInfo> = _stockInfo

    fun get(secid: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val stockSF = easy.getStockSF(secid)
            if (stockSF != null) {
                _stockInfo.postValue(stockSF!!)
            }

        }
    }

    class KlineViewModelFactory(private val easy: EasyMoneyRepository) : ViewModelProvider.Factory {
        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            if (modelClass.isAssignableFrom(KlineViewModel::class.java)) {
                return KlineViewModel(easy) as T
            } else throw ClassCastException("我不要你${modelClass.canonicalName}")
        }

    }
}